const arrayPrototype = Array.prototype;
const arrayMethods = Object.create(arrayPrototype);
const methodsNeedChange=[
    "push",
    "pop",
    "shift",
    "unshift",
    "splice",
    "sort",
    "reverse",
]
methodsNeedChange.forEach((methodName)=>{
    //备份原有的方法
    const original = arrayPrototype[methodName];
    //定义新的方法
    Object.defineProperty(arrayMethods,methodName,{
        value:function(){
            const args=[...arguments]
            const ob =this.__ob__;
            let inserted=[];
            switch(methodName){
                case"push":
                case "unshift":
                    inserted =  args;
                    break;
            //splice格式是 splice(下标，数量，插入元素)
                case "splice":
                    inserted =  args.slice(2);
                    break;
            }
            if(inserted.length>0){
                //对新加入元素要observe
                ob.observeArray(inserted)
            }
            //this指向调用对象---数组
            const result=original.apply(this, args);
            ob.dep.notify();
            return result;
        }
    })
})
export default arrayMethods;